package com.daguchuangyi.erp_android.production_detail;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.ErpApplication;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.model.product.Photo;
import com.daguchuangyi.erp_android.util.ImageUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProductImageFragment extends Fragment {

    private static final String TAG_PHOTO = "TAG_PHOTO";
    private static final String TAG_PRODUCT_PNO = "TAG_PRODUCT_PNO";

    private String mPNOName = "";
    private ArrayList<Photo> mPhotos = new ArrayList<>();
    private RecyclerView mImgRv;

    private ImageAdapter mAdapter;

    public ProductImageFragment() {
        // Required empty public constructor
    }

    public static ProductImageFragment getInstance(ArrayList<Photo> photos, String PNOName) {
        ProductImageFragment imageFragment = new ProductImageFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(TAG_PHOTO, photos);
        bundle.putString(TAG_PRODUCT_PNO, PNOName);
        imageFragment.setArguments(bundle);
        return imageFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (getArguments() != null) {
            mPhotos.clear();
            mPhotos.addAll(getArguments().getParcelableArrayList(TAG_PHOTO));
            mPNOName = getArguments().getString(TAG_PRODUCT_PNO);
        }
        return inflater.inflate(R.layout.fragment_product_image, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mImgRv = view.findViewById(R.id.product_img_rv);
        mImgRv.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mAdapter = new ImageAdapter(R.layout.item_product_detail_img_layout, null);
        mImgRv.setAdapter(mAdapter);
//        mImgRv.setNestedScrollingEnabled(true);

        mAdapter.setNewData(mPhotos);
    }

    private class ImageAdapter extends BaseQuickAdapter<Photo, BaseViewHolder> {

        public ImageAdapter(int layoutResId, @Nullable List<Photo> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, Photo item) {
            ImageView imageView = helper.getView(R.id.product_detail_item_img_iv);
            Glide.with(ErpApplication.getAppContext())
                    .applyDefaultRequestOptions(ImageUtil.getGlideRequestOptions())
                    .load(item.getImgUrl())
                    .into(imageView);
            helper.itemView.setOnClickListener(view -> BigPictureActivity.start(getContext(), item.getImgUrl(), mPNOName + System.currentTimeMillis()));
        }
    }

}
